package UI;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JSeparator;

/*
 *
 * @author Christophe
 * This programme is metioned to create logical gates and digital simulation.
 * This programe was assembled by Heck Christophe, Zuné Gary, Ibrahimovic Seid 
 * and Warnimont Pol as project for the BTSI.
 */
public class MainFrame extends JFrame {

    protected boolean saved = false;

    /**
     * Constructor of the main Frame
     *
     * @param title name of the program
     */
    public MainFrame(String title) {
        super(title);
        setBounds(25, 25, 1280, 720);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        initMenu();

        /*
         * MainPanel added to the MainFrame
         */
        MainPanel mainPanel = new MainPanel();
        add(mainPanel);
    }

    /*
     * Opens the Interface where all the panels were added
     */
    public static void main(String[] args) {
        MainFrame main = new MainFrame("Digital Simulator");
        main.setVisible(true);
    }

    /*
     * Get the status of the saved variable to confirm if the current sheet
     * is already saved or not
     */
    public boolean isSaved() {
        return saved;
    }

    /*
     * Set the save status to true or false depending on the changes which were
     * done
     */
    public void setSaved(boolean saved) {
        this.saved = saved;
    }

    /*
     * Initialise the menu of the digital simulator. On this function you can 
     * add you menu points which were needed.
     */
    private void initMenu() {
        
        /*
         * Creation of the MenuBar
         */
        JMenuBar myMenuBar = new JMenuBar();

        /*
         * Add the menupoints which are needed
         */
        JMenu fileMenu = new JMenu("File");
        JMenu aboutMenu = new JMenu("Help");

        /*
         * The items to the file menu
         */
        JMenuItem fileNew = new JMenuItem("New");
        JMenuItem fileOpen = new JMenuItem("Open");
        JMenuItem fileSave = new JMenuItem("Save");
        JMenuItem fileSaveAs = new JMenuItem("Save as...");
        JMenuItem fileClose = new JMenuItem("Close");

        /*
         * On this part all the file menu the ActionListner to every file 
         * submenu is set
         */
        fileNew.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (isSaved()==true)
                {
                    System.out.println("Create new mainpanel");
                }
                else
                {
                    System.out.println("Open save dialogue in this case");
                }
            }
        });
        
        /*
         * Opens an already existing file, in case that the new file wasn't 
         * saved yet a dialog will open to ask if we want to save the current
         * file
         */
        fileOpen.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (isSaved()==true)
                {
                    System.out.println("Open an other file");
                }
                else
                {
                    System.out.println("Open save dialogue in this case");
                }
            }
        });
        
        /*
         * Here in this cae we need to check if the current file was already 
         * saved if not the SaveAs... Dialogue will be opened
         */
        fileSave.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (isSaved()==true)
                {
                    System.out.println("Open an other file");
                }
                else
                {
                    System.out.println("Open save dialogue in this case");
                }
            }
        });
        
        /*
         * Saves the current file
         */
        fileSaveAs.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("Save the Current file");
            }
        });
        
        /*
         * Close the window of the DigitalSimulator
         */
        fileClose.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
    
        /*
         *  Set items of the Help menu
         */
        JMenuItem fileAbout = new JMenuItem("About");

        /*
         * Set the menu that it is visible
         */
        fileMenu.add(fileNew);
        fileMenu.add(fileOpen);
        fileMenu.add(fileSave);
        fileMenu.add(fileSaveAs);
        fileMenu.add(new JSeparator());
        fileMenu.add(fileClose);
        aboutMenu.add(fileAbout);
        myMenuBar.add(fileMenu);
        myMenuBar.add(aboutMenu);
        setJMenuBar(myMenuBar);
    }
    
}